package org.ariadne_eu.hcifetcher.servlet;

import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ariadne_eu.hcifetcher.rest.RestUtils;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;

public class AddPointsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final Logger log = Logger.getLogger(AddPointsServlet.class.getName());
	private DatastoreService datastore;

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		datastore = DatastoreServiceFactory.getDatastoreService();
		String studentid = req.getParameter("studentid");
		String score = req.getParameter("score");
		String courseid = req.getParameter("courseid");
		int intscore = Integer.parseInt(score);
		String responseid = req.getParameter("responseid");
		String response = "";
		if(courseid != null)
			response = updateEnrollmentWithScore(studentid,courseid,intscore);
		else if(responseid != null)
			response = updateResponseWithScore(studentid, responseid,intscore);
		resp.getWriter().write(response);
		
	}
	
	private String updateEnrollmentWithScore(String studentid, String courseid, int score){
		// Enrollment ophalen dmv een query
		Entity enrollment;
		Query q = new Query("Enrollment");
		q.addFilter("studentid", FilterOperator.EQUAL, studentid);
		q.addFilter("courseid", FilterOperator.EQUAL, courseid);
		PreparedQuery pq = datastore.prepare(q);
		List<Entity> results = pq.asList(FetchOptions.Builder.withLimit(1));
		enrollment = results.get(0);
		Long orgscore = (Long) enrollment.getProperty("score");
		long totscore = score + orgscore;
		enrollment.setProperty("score", totscore);
		datastore.put(enrollment);
		log.log(Level.INFO, "Added points for " + studentid + " to course: " + courseid + "with score: " + score + ".");
		updateStudentWithScore(studentid, score);
		return "Added points for " + studentid + " to course: " + courseid + " with score: " + score + ".";
		
	}
	
	private String updateResponseWithScore(String studentid, String responseid, int score){
		RestUtils ruts = new RestUtils();
		Entity answer = ruts.getSingleEntity(responseid);
		Long orgscore = (Long) answer.getProperty("score");
		long totscore = score + orgscore;
		answer.setProperty("score", totscore);
		datastore.put(answer);
		log.log(Level.INFO, "Added points for " + responseid + "with score: " + score + ".");
		updateStudentWithScore(studentid, score);
		return "Added points for " + responseid + "with score: " + score + ".";
	}
	
	private void updateStudentWithScore(String studentid, int score) {
		RestUtils ruts = new RestUtils();
		Entity student = ruts.getSingleEntity(studentid);
		Long orgscorestud = (Long) student.getProperty("score");
		long totstudscore = orgscorestud + score;
		student.setProperty("score", totstudscore);
		if(datastore != null)
			datastore.put(student);
	}

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		doPost(req, resp);
	}
}
